﻿#region

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using JsMapper.BO;

#endregion

namespace JsMapper.StandaloneExporter
{
    public partial class JsMapperExporterForm : Form
    {
        public JsMapperExporterForm()
        {
            InitializeComponent();
            SetMapperComboBox();
        }

        private void SetMapperComboBox()
        {
            var mappers = JsMapperRepository.GetAll()
                                            .Select(mapper => new KeyValuePair<string, IJsModelMapper>(mapper.GetType().Name, mapper));

            ComboBoxMappers.DisplayMember = "Key";
            ComboBoxMappers.ValueMember = "Value";
            ComboBoxMappers.DataSource = new BindingSource(mappers, null);
        }

        private void ButtonBrowseAssembly_Click(object sender, EventArgs e)
        {
            var fileDialog = new OpenFileDialog {Filter = "Assembly|*.dll"};
            var dialogResult = fileDialog.ShowDialog();

            if (dialogResult != DialogResult.OK) return;

            TextBoxAssembly.Text = fileDialog.FileName;
        }

        private void GetConfigValues(out string assemblyPath, out IJsModelMapper mapper, out bool wrapOutput)
        {
            assemblyPath = TextBoxAssembly.Text;
            mapper = ((KeyValuePair<string, IJsModelMapper>) ComboBoxMappers.SelectedItem).Value;
            wrapOutput = CheckboxWrappedExport.Checked;
        }

        private void Button1FilePerModel_Click(object sender, EventArgs e)
        {
            var folderBrowser = new FolderBrowserDialog {Description = "Select Folder to save .js-Files"};
            var browserResult = folderBrowser.ShowDialog();
            if (browserResult != DialogResult.OK) return;

            var outputPath = folderBrowser.SelectedPath;
            string assemblyPath;
            IJsModelMapper mapper;
            bool wrapOutput;
            GetConfigValues(out assemblyPath, out mapper, out wrapOutput);

            Exporter.Export1FilePerModel(assemblyPath, outputPath, wrapOutput, mapper);

            var exportDoneResult = MessageBox.Show("Export is done.\nWould you like to open the Folder?", "Export finished", MessageBoxButtons.YesNo);
            if (exportDoneResult != DialogResult.Yes) return;

            Process.Start(outputPath);
        }


        private void ButtonCreate1File_Click(object sender, EventArgs e)
        {
            var filePathBrowser = new SaveFileDialog
                {
                    Title = "Save .js-File",
                    CheckPathExists = true,
                    DefaultExt = "js",
                    Filter = "Javascript File (*.js)|*.js|All files (*.*)|*.*"
                };
            var filePathResult = filePathBrowser.ShowDialog();

            if (filePathResult != DialogResult.OK) return;

            var outputPath = filePathBrowser.FileName;
            string assemblyPath;
            IJsModelMapper mapper;
            bool wrapOutput;
            GetConfigValues(out assemblyPath, out mapper, out wrapOutput);

            Exporter.Export1File(assemblyPath, outputPath, wrapOutput, mapper);

            var exportDoneResult = MessageBox.Show("Export is done.\nWould you like to open the File?", "Export finished", MessageBoxButtons.YesNo);
            if (exportDoneResult != DialogResult.Yes) return;

            Process.Start(outputPath);
        }
    }
}